var colHarnessId   = null;
var colHarnessDash = null;
var eqLookupWindow;
var targetWindow;
var tempStr = '';

function moveRow( from ) 
{
  var mytable = document.getElementById("dataTable1");
  //mytable.moveRow(0, mytable.rows.length-1) //move first row to the end of the table instead

  mytable.moveRow(from, -1); //Alternate way to move first row to the end of the table
}


function toggleDisplay(id)
{  
  var myObject = document.getElementById(id);

  //alert( myObject.id + " Current Style : " + myObject.style.display);
  if ( myObject.style.display == "none" ) {
     myObject.style.display = "inline";
  }
  else {
     myObject.style.display = "none"; 
  }

}


  
function toggleObjectDisplay(myObject)  
{  
  if ( myObject != null ) {      
     //alert( myObject.id + " Current Style : " + myObject.style.display);
     if ( myObject.style.display == "none" ) {
        myObject.style.display = "inline";
     }
     else {
        myObject.style.display = "none"; 
     }
  }   
  
} //function



function toggleVisibility(id) 
{
  var myObject = document.getElementById(id);
  
  if ( myObject.style.visibility == "collapse" ) {
     myObject.style.visibility = "visible";
  }
  else {
     myObject.style.visibility = "collapse";
  }
}  


function toggleObjectVisibility(myObject) 
{
  if ( myObject != null ) {    
     if ( myObject.style.visibility == "collapse" ) {
        myObject.style.visibility = "visible";
     }
     else {
        myObject.style.visibility = "collapse";
     }
  }
}  


function toggleTab(tabId, targetId)
{
  var tabObject = document.getElementById(tabId); 
  //alert(tabObject + " src: " + tabObject.src + " Value: " + tabObject.value);
  if ( tabObject.value != 1 ) {     
     tabObject.src = "./image/arrowDown.gif";
     tabObject.value = 1;
  }
  else {
     tabObject.src = "./image/arrowUp.gif";       
     tabObject.value = 0;
  }

  if (window.screen && window.screen.pixelDepth) {
     toggleVisibility(targetId);  
  }
  else {
     toggleDisplay(targetId);  
  }
     
}


function toggleColumnDisplay(table, colIndex) {
   //alert( table + " : " +  colIndex);
   
   colIndex--;
         
   var rows = table.getElementsByTagName("tr");
   var numOfRow = rows.length;
   var col_TH = null;
   var col_TD = null;
   var i = 0;
   
   //alert( "all tr : " + rows + " length : " + numOfRow);
   
   col_TH = rows[0].getElementsByTagName("th");
   
   //alert( "th row : " + col_TH + " length : " + col_TH.length);
   
   if (window.screen && window.screen.pixelDepth) {
      toggleObjectVisibility(col_TH[colIndex]);
   }
   else {
      toggleObjectDisplay(col_TH[colIndex]);
   }
   
   for ( i = 1; i < numOfRow; i++ ) {
      col_TD = rows[i].getElementsByTagName("td");     
      
      if (window.screen && window.screen.pixelDepth) {
         toggleObjectVisibility(col_TD[colIndex]);
      }
      else {
        toggleObjectDisplay(col_TD[colIndex]);
      }
      
   } // for

} // function


function toggleTableColumn(tableId, colIndex) {
   var table = document.getElementById('form1:dataTable1');
         
   toggleColumnDisplay(table, colIndex);      
}


function resetForm() {
   //document.getElementById('form1').reset(); 
   document.getElementById('txtFromLineNum').value="";      
   document.getElementsByName('radHarness')[1].checked = true;
   radHarnessClick();
}          


function radEquipClick() {
   document.getElementById('radEquipment').style.fontWeight = '700'; 
   document.getElementById('radHarness').style.fontWeight   = 'normal';  
   
   if (window.screen && window.screen.pixelDepth) {  //Check for Firefox
      document.getElementsByName('radHarness')[0].checked = false;
   }
   else {
      document.getElementsByName('radHarness')[1].checked = false;
   }
   
   document.getElementById('lstHarness').disabled           = true;
   document.getElementById('txtEquipment').style.background = "white";
   document.getElementById('txtEquipment').disabled         = false;
   document.getElementById('btnEQLookup').disabled          = false;
}                          
          
          
function radHarnessClick() {  
   var lstHarness = document.getElementById('lstHarness');
   var msg = "Please wait.....";
  
   document.getElementById('checkHarnessList').value = "true";
   
   if ( validateForm(true) ) {
      document.getElementById('radHarness').style.fontWeight = "700";                           
      document.getElementById('radEquipment').style.fontWeight = "normal";  
      
      if (window.screen && window.screen.pixelDepth) {  //Check for Firefox
         document.getElementsByName('radEquipment')[0].checked = false;
      }
      else {
         document.getElementsByName('radEquipment')[1].checked = false;
      }
      
      lstHarness.disabled           = false;
      lstHarness.style.background   = "white";
      document.getElementById('txtEquipment').style.background = "#EEE9E9";
      document.getElementById('txtEquipment').disabled         = true;    
      document.getElementById('btnEQLookup').disabled          = true; 
           
      if ( lstHarness.length == 0 ) {
         appendOptionLast(lstHarness, msg);
      }
      lstHarness.selectedIndex = 0
      lstHarness.options[0].text = msg;
      document.getElementById('form1').submit();
   }
   else {
      if (window.screen && window.screen.pixelDepth) {  //Check for Firefox
         document.getElementsByName('radHarness')[0].checked = false;
      }
      else {
         document.getElementsByName('radHarness')[1].checked = false;
      }
      lstHarness.disabled = true;
   }
}                          



function appendOptionLast(list, value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = value;
  elOptNew.value = value;

  try {
    list.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    list.add(elOptNew); // IE only
  }
}



function validateForm(isLineNumOnly)
{        
   var lineNum = trim(document.getElementById("txtFromLineNum").value);
   document.getElementById("txtFromLineNum").value = lineNum; 
   
   if ( lineNum == "" || lineNum.length < 3 ) {      
      alert( "Line Number is a numeric field that requires a minimum 3-digit value. (e.g. 001)" );     
      document.getElementById("txtFromLineNum").focus();
      isFromAlertWindow = true;  // Work around for Mozilla bug.
      return false;
   }
   
   if ( isNaN(lineNum) || lineNum < 0 ) {
      alert( "The Line Number field only accepts numeric characters.  Please re-enter a valid numeric value. (e.g. 001)" );         
      document.getElementById("txtFromLineNum").focus();
      isFromAlertWindow = true;  // Work around for Mozilla bug.
      return false;
   }
   
   if ( !isLineNumOnly ) {
      document.getElementById("txtEquipment").value = trim(document.getElementById("txtEquipment").value); 
       
      if ( document.getElementById("form1").radHarness.checked == false && document.getElementById("form1").radEquipment.checked == false ) {
         alert( "Please select a Harness Number or enter an Equipment Number to query." );         
         document.getElementById("form1").radHarness.focus();
         isFromAlertWindow = true;  // Work around for Mozilla bug.
         return false;
      }
   
      if ( document.getElementById("form1").radHarness.checked == true && document.getElementById("lstHarness").value == "" ) {
         alert( "Please select a Harness Number to query." );         
         document.getElementById("lstHarness").focus();
         isFromAlertWindow = true;  // Work around for Mozilla bug.
         return false;
      }
   
      if ( document.getElementById("form1").radEquipment.checked == true && document.getElementById("txtEquipment").value == "" ) {
         alert( "Please enter an Equipment Number to query." );         
         document.getElementById("txtEquipment").focus();
         isFromAlertWindow = true;  // Work around for Mozilla bug.
         return false;
      }
   }      
   
   return true;
   
}



function processEQLookup(windowId) {
   
   if ( eqLookupWindow && eqLookupWindow.open && !eqLookupWindow.closed ) {
         eqLookupWindow.focus();
   }
   else {
      eqLookupWindow = window.open( "QueryEquipLookupJSF.jsp"
                                  , windowId
                                  , "height=700,width=815,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");  
   }
              
   return false;                 
}     



function closeEQLookupWindow() {

   if ( eqLookupWindow && eqLookupWindow.open && !eqLookupWindow.closed ) {
         eqLookupWindow.close();
   }

}





function openNewWindow(url) {
   
   if ( targetWindow && targetWindow.open && !targetWindow.closed ) {
      targetWindow.focus();
      return false;
   }
   else {
      targetWindow = window.opener.openWindow(url, "secondHarnessWindow");
   }
   
   return false;
            
}     


function hideTab(id) {   
   
   if (window.screen && window.screen.pixelDepth) {
      document.getElementById(id).style.visibility = "collapse";
   }
   else {
      document.getElementById(id).style.display = "none";
   }
   
}


/*
* Work-around for display attribute of JSP framework.
*/
function setFieldAttribute() {
   if (window.screen && window.screen.pixelDepth) {  //Check for Firefox
      if ( document.getElementsByName('radEquipment')[0].checked ) { 
         document.getElementById('txtEquipment').style.background = "white";
         document.getElementById('txtEquipment').disabled = false; 
      }
      else {
         document.getElementById('txtEquipment').style.background = "#EEE9E9";
         document.getElementById('txtEquipment').disabled = true; 
      }
   }
   else {
      if ( document.getElementsByName('radEquipment')[1].checked ) { 
         document.getElementById('txtEquipment').style.background = "white";
         document.getElementById('txtEquipment').disabled = false; 
      }
      else {
         document.getElementById('txtEquipment').style.background = "#EEE9E9";
         document.getElementById('txtEquipment').disabled = true; 
      }
   }
}



function processOnload() {
   
   if (document.getElementById('initialLoad').value == "" ) {
      //var parentModelField = window.opener.document.getElementById("selModel");
      //document.getElementById("txtModel").value = parentModelField.value;
      document.getElementById('initialLoad').value = "false";     
   }
   
   document.getElementById("txtFromLineNum").setAttribute("autocomplete", "OFF");
   document.getElementById("txtEquipment").setAttribute("autocomplete", "OFF");
   
   document.getElementById('txtFromLineNum').focus();   
   setFieldAttribute(); hideTab('tabTable'); 
   history.go(1);
}

function handleResize() {
   document.getElementById('form1:dataTable1').tHead.firstChild.style.position='relative';
   document.getElementById('form1:dataTable1').tHead.firstChild.style.top=document.getElementById('tableContainer').scrollTop;
}

function resetAction() {
   radObj = document.getElementById('lstAction');
   radObj.options[radObj.selectedIndex].text = tempStr;
   radObj.value = '';
}


function handleLineNumKeyDown() {
   if (window.screen && window.screen.pixelDepth) {  //Check for Firefox
      document.getElementsByName('radHarness')[0].checked = false;
   }
   else {
      document.getElementsByName('radHarness')[1].checked = false;
   }
   document.getElementById('lstHarness').disabled=true;
   
}